//
//  UpdateVC.m
//  doubleEduStudents
//
//  Created by 盛洋 on 2021/1/8.
//  Copyright © 2021 shengYang. All rights reserved.
//

#import "UpdateVC.h"

@interface UpdateVC ()

@end

@implementation UpdateVC{
    RequestServices *service;
    NSString *currentVersion;//当前版本号(去掉.)
    NSString *appCurVersionName;//当前版本名称
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"版本更新";
    [self.backKuang setHidden:YES];
    [self showBackCenterKuang:[UIImage imageNamed:@"back_center1"]];
    self.topHeight.constant = StatusBarAndNavHeight + 90;
//    APPViewBorderRadius(self.update_btn, self.update_btn.frame.size.height/2, .1, [UIColor clearColor]);
    self.update_btn.titleLabel.font = self.version_lab.font = AutoLabFont2;
    [self.update_btn setHidden:YES];
    [self.update_btn addTarget:self action:@selector(pushUpdate) forControlEvents:UIControlEventTouchUpInside];
    //获取当前版本号
    appCurVersionName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    self.version_lab.text = [NSString stringWithFormat:@"当前版本号V%@",appCurVersionName];
    currentVersion = [appCurVersionName
    stringByReplacingOccurrencesOfString:@"." withString:@""];
    [self ShowLoading:@"检测中"];
    
    
    service  = [[RequestServices alloc]initWithDelegate:self];
    [service checkVersion:@"2" version:currentVersion tag:8000];
}

-(void)pushUpdate{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://:apps.apple.com/gb/app/yi-dong-cai-bian/id391945719"]];
}

#pragma mark - requestDelegate
-(void)getFinished:(NSDictionary *)obj tag:(long long)tag{
    [self HideLoading];
    if ([[obj[@"versionCode"] stringValue] isEqualToString:currentVersion]) {
        self.version_lab.text = [NSString stringWithFormat:@"当前版本号V%@  (已是最新)",appCurVersionName];
        [self.update_btn setHidden:YES];
    }else{
        self.version_lab.text = [NSString stringWithFormat:@"当前版本号V%@  (检测到最新版本V%@)",appCurVersionName,obj[@"versionName"]];
        [self.update_btn setHidden:NO];
    }
}

-(void)getError:(NSError *)error tag:(long long)tag{
    [self HideLoading];
    [self ShowNewMsg:error.domain];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
